package com.fengmang.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;

import butterknife.BindView;
import butterknife.ButterKnife;


public class TestRoomAActivity extends Activity implements OnClickListener {
    
	/**
	 * TestHallActivity的上下文引用
	 */
	private Activity otherActivity;
	private static View view;

//	private static RoundImageView view2;

	@BindView(R.id.ll_table_head)		//N
	LinearLayout ll_table_head;

	/**
	 * 无参构造函数
	 */
	public TestRoomAActivity() {
		super();
	}
	
	/**
	 * 有参构造函数
	 * @param context
	 */
	public TestRoomAActivity(Context context) {
		super();
		otherActivity = (Activity) context;
	}

	private static void setLayout(View v){
		view = v;
	}


	@Override
    public void onCreate(Bundle savedInstanceState) {
		Log.e("tra===0", "==="+otherActivity);

		try {
			if (otherActivity != null) {
				// 使用TestHallActivity的上下文引用创建View并添加到根视图
//			Button button = new Button(otherActivity);
//			button.setText("Room A2");
//			button.setOnClickListener(this);
//
//			LinearLayout root = new LinearLayout(otherActivity);
//
//			root.addView(button);
//
//			// setContentView(root);使用的上下文是当前Activity的，而不是指定的TestHallActivity
//			otherActivity.setContentView(root);



//			otherActivity.setContentView(R.layout.activity_main2);
				otherActivity.setContentView(view);

				ButterKnife.bind(otherActivity);	//N

//				new RoundImageView(otherActivity);

//				LinearLayout ll_table_head = (LinearLayout) otherActivity.findViewById(R.id.ll_table_head);

				FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);   //FrameLayout.LayoutParams.WRAP_CONTENT：表示包住文字的宽高
				params.setMargins(100, 200, 0, 0);    //设置文字的文字left、top、right、bottom， 注：right、bottom没用

				Log.e("tra===", "==="+ll_table_head);	 //null

				RoundImageView rview = new RoundImageView(otherActivity);
				rview.setImageResource(R.mipmap.t2);
//				ll_table_head.addView(rview);
          		ll_table_head.addView(new RoundImageView(this), params);

//				Toast.makeText(otherActivity, "That is Room "+new B().test(), Toast.LENGTH_SHORT).show();
			} else {
				super.onCreate(savedInstanceState);
				setContentView(R.layout.activity_main4);

				Log.e("tra===2", "==="+this);

				Toast.makeText(this, "xxx", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			Log.e("tra===e", "==="+e);
			e.printStackTrace();
		}


//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_main);
    }
    
    /**
     * 返回当前Activity的描述信息
     * @return
     */
    private String getRoomMessage() {
    	return "Room A";
    }

	@Override
	public void onClick(View v) {
		
		if (otherActivity != null) {
			// Toast.makeText(this, "This is Room A!", Toast.LENGTH_SHORT).show();是不合适的调用
			Toast.makeText(otherActivity, "This is Room A!", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "This is Room A!", Toast.LENGTH_SHORT).show();
		}
		
	}

//	//重写加载资源
//	@Override
//	public Resources getResources() {
////		Log.e("p_getResources===", super.getResources()+"==="+PluginManager.getInstance());
//
//		Resources resources = null;
//
//		try {
//			Context context = getApplicationContext();
//			Log.e("getResources===1", resources+"==="+context.getResources());
//
//			AssetManager manager = AssetManager.class.newInstance();
//			Method addAssetPath = AssetManager.class.getMethod("addAssetPath", String.class);
//			addAssetPath.invoke(manager, "/data/data/com.fengmang.myapplication/app-debug.apk");
//			resources = new Resources(manager, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());
//
//			Log.e("getResources===2", resources+"==="+context.getResources());
//		} catch (Exception e) {
//			Log.e("getResources===e", "==="+e);
//			e.printStackTrace();
//		}
//
//
//		return resources;
////		return PluginManager.getInstance().getResources()==null?super.getResources():PluginManager.getInstance().getResources();
//	}

	//重写加载资源
	@Override
	public Resources getResources() {
		Log.e("getResources===", super.getResources()+"==="+PluginManager.getInstance());
		return PluginManager.getInstance().getResources()==null?super.getResources():PluginManager.getInstance().getResources();
	}

}
